(function() {
    angular.module('app').component('attrHistoryComponent', {
        templateUrl: 'app/controllers/attr.controller/attr.history.component/attr.history.component.html',
        controller: attrHistoryComponent,
        controllerAs: 'vm'
    });

    attrHistoryComponent.$inject = ['attrHistoryComponentService', '$rootScope', '$scope', 'util','$loading'];

    function attrHistoryComponent(attrHistoryComponentService, $rootScope, $scope, util,$loading) {
        var vm = this;

        vm.seen = false;

        vm.json = {};

        vm.title = '属性组合销售额历史变化趋势';

        vm.dashCopy = [{
            icon: 'icon line chart ui teal-text', //icon
            title: '销售额', //title
            date: '', //date
            body: 'sellMoney', //字段
            footer: 'sellMoneyGrowth'
        }, {
            icon: 'icon bar chart ui', //icon
            title: '销售量', //title
            date: '', //date
            body: 'sellAmount',
            footer: 'sellAmountGrowth'
        }, {
            icon: 'dollar icon ui pink-text', //icon
            title: '成交均价', //title
            date: '', //date
            body: 'avg'
        }, {
            icon: 'shopping bag icon ui red-text', //icon
            title: '宝贝数', //title
            date: '', //date
            body: 'productNum'
        }];

        // vm.dash = 

        vm.option = {

            tooltip: {
                trigger: 'axis',
                axisPointer: { // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: function(params) {
                    var str = '<div style = "text-align: left;">' + params[0].name + '</div>';

                    for (var i = 0; i < params.length; i++) {
                        str += '<div style = "text-align: left;padding-left:10px;">' + params[i].seriesName + ' : ' + util.bigNumFormat(params[i].data) + '</div>';
                    }

                    return str;
                }
            },
            legend: {
                data: [],
                y: 'bottom'
            },
            grid: {
                left: '60'
                // right: '40',
                // bottom: '20',
                // containLabel: true
            },
            toolbox: {},
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: [],
                offset: 10
            },
            yAxis: [{
                type: 'value',
                axisLabel: {
                    formatter: function(value, index) {
                        var len = String(value).length;

                        if (value < 10000) {
                            return value;
                        }

                        switch (len) {
                            case 5:
                                return (value / 10000).toFixed(1) + '万';
                            case 6:
                                return (value / 10000).toFixed(1) + '万';
                            case 7:
                                return (value / 10000).toFixed(1) + '万';
                            case 8:
                                return (value / 10000).toFixed(1) + '万';
                            case 9:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 10:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 11:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 12:
                                return (value / 100000000).toFixed(2) + '亿';
                        }

                        return value;
                    }
                }
            }],
            series: [{
                name: '销售额',
                type: 'line',
                data: [],
                markPoint: {
                    symbol: 'circle'
                }
            }, {
                name: '销售量',
                type: 'line',
                data: [],
                markPoint: {
                    symbol: 'circle'
                }
            }]
        };

        var clearFun = $rootScope.$on('emit_attr_history_component_EV', function(e, filter, selected_props, type) {

            // console.log("---------------emit_attr_history_component_EV $onz ---------");
            // console.log('selected_props:'+selected_props);
            if(selected_props.length==0){
                vm.seen = false;
                // console.log('no show');
                return;
            }

            // console.log('arr00000000000000000000');
            vm.json = {};
            vm.json = {
                category: filter.category,
                property: selected_props,
                shopType: filter.shopType,
                type: type
            };

            // console.log(vm.json);

            init(vm.json);
        });

        var hideEV = $rootScope.$on('pool_emit_hide_EV',function(){

            vm.seen = false;

        });

        $scope.$on('$destroy', function() {
            clearFun();
            hideEV();
        });

        // init();


        function init(json) {

            $loading.start('attrhistory');
            
            if(!vm.seen){
                vm.seen = true;
            }
            

            $rootScope.$emit('echart_draw_line_loading_EV','drawC');
            attrHistoryComponentService.get(json).then(function(data) {

                // console.log(data);

                // console.log(dim);

                var option = angular.copy(vm.option);
                if (data.length > 0) {
                    
                    var dim = _getDataFormatFromShopType(json.shopType, data[data.length - 1]);

                    angular.forEach(data, function(item, index) {
                        option.xAxis.data.push(item.date.slice(0, 10)); //"2017-01-20 00:00:00"
                        option.series[0].data.push(item[dim[0]]);
                        option.series[1].data.push(item[dim[1]]);
                    });

                    $rootScope.$emit('echart_draw_line_EV', option, 'drawC');


                    $loading.finish('attrhistory');
                    // _dashCardConfigInit(data[data.length - 1],dim);

                } else {
                    vm.seen = false;
                    $loading.finish('attrhistory');
                }


            }, function(err) {
                $loading.finish('attrhistory');
            });
        }

        /**
         * [_getDataFormatFromShopType description]
         * @param  {[type]} shopType [description]
         * @param  {[type]} lastData [最后一个数据数]
         * @return {[type]}          [description]
         */
        function _getDataFormatFromShopType(shopType, lastData) {
            var dim = [];
            vm.dash = angular.copy(vm.dashCopy);
            switch (shopType) {
                case '': //全网
                    dim = ['sellMoney', 'sellAmount'];

                    vm.dash[0].body = "sellMoney";
                    vm.dash[0].footer = "sellMoneyGrowth";
                    vm.dash[1].body = "sellAmount";
                    vm.dash[0].footer = "sellAmountGrowth";

                    vm.dash[3].body = "productNum";

                    _dashCardConfigInit(lastData);
                    break;
                case 'C店':
                    dim = ['cSellMoney', 'cSellAmount'];

                    vm.dash[0].body = "cSellMoney";
                    vm.dash[0].footer = "cSellAmountGrowth";
                    vm.dash[1].body = "cSellAmount";
                    vm.dash[0].footer = "cSellMoneyGrowth";

                    vm.dash[3].body = "cProductNum";
                    _dashCardConfigInit(lastData);
                    break;
                case '商城':
                    dim = ['tSellMoney', 'tSellAmount'];

                    vm.dash[0].body = "tSellMoney";
                    vm.dash[0].footer = "tSellAmountGrowth";
                    vm.dash[1].body = "tSellAmount";
                    vm.dash[0].footer = "tSellMoneyGrowth";

                    vm.dash[3].body = "tProductNum";
                    _dashCardConfigInit(lastData);
                    break;
                case '天猫国际':
                    dim = ['thkSellMoney', 'thkSellAmount'];

                    vm.dash[0].body = "thkSellMoney";
                    vm.dash[0].footer = "";
                    vm.dash[1].body = "thkSellAmount";
                    vm.dash[0].footer = "";

                    vm.dash[3].body = "thkProductNum";

                    _dashCardConfigInit(lastData);
                    break;

                case '全球购':
                    dim = ['gSellMoney', 'gSellAmount'];

                    vm.dash[0].body = "gSellMoney";
                    vm.dash[0].footer = "";
                    vm.dash[1].body = "gSellAmount";
                    vm.dash[0].footer = "";

                    vm.dash[3].body = "gProductNum";
                    _dashCardConfigInit(lastData);
                    break;
                default:
                    break;
            }

            return dim;
        }


        /*
        vm.dash = [{
            icon: 'icon line chart ui teal-text', //icon
            title: '销售额', //title
            date: '', //date
            body: 'sellMoney', //字段
            footer: 'sellMoneyGrowth'
        }, {
            icon: 'icon bar chart ui red-txet', //icon
            title: '销售量', //title
            date: '', //date
            body: 'sellAmount',
            footer: 'sellAmountGrowth'
        }, {
            icon: 'home icon ui green-text', //icon
            title: '成交均价', //title
            date: '', //date
            body: 'avg'
        }, {
            icon: 'shopping bag icon ui red-text', //icon
            title: '宝贝数', //title
            date: '', //date
            body: 'productNum'
        }];
         */
        function _dashCardConfigInit(data) {

            angular.forEach(vm.dash, function(dash, index) {
                dash.date = data.date.slice(0, 10);

                if (dash.body == 'avg') {
                    // dash.body = 
                } else {
                    dash.body = data[dash.body];
                }

                dash.footer = typeof(dash.footer) == 'undefined' ? '' : data[dash.footer];
            });

            if (vm.dash[1].body == 0) {
                vm.dash[2].body = 0;
            } else {
                vm.dash[2].body = Number((vm.dash[0].body / vm.dash[1].body).toFixed(2));
            }

            $rootScope.$emit('emit_dash_card_EV',vm.dash);
        }

    }

})();
